
import 'package:flutter/material.dart';
import 'package:up_demo/time.dart';
import 'package:up_demo/widget_extension.dart';

import 'multiple_choice_time.dart';

/// 选取时间段 返回开始时间和结束时间
/// [onConfirm] 开始时间 结束时间
/// [startTime] 选择的开始时间
/// [endTime] 选择的结束时间
class CustomSelectTime extends StatefulWidget {
  const CustomSelectTime({
    required this.onConfirm,
    this.startTime,
    this.endTime,
    super.key,
  });

  final Function(DateTime? startTime, DateTime? endTime) onConfirm;
  final DateTime? startTime;
  final DateTime? endTime;

  @override
  State<CustomSelectTime> createState() => _CustomSelectTimeState();
}
class _CustomSelectTimeState extends State<CustomSelectTime> {

  // 开始时间
  DateTime? _startDate;

  // 结束时间
  DateTime? _endDate;

  @override
  initState() {
    super.initState();
    if (widget.startTime != null) {
      _startDate = widget.startTime!;
    }
    if (widget.endTime != null) {
      _endDate = widget.endTime!;
    }
  }

  // 选择时间区间
  void _selectTime(BuildContext context) {
    DateRangePicker.show(
      context: context,
      initialStartDate: _startDate??DateTime.now().subtract(const Duration(days: 15)),
      initialEndDate: _endDate??DateTime.now(),
      minDate: DateTime.now().subtract(const Duration(days: 90)),
      maxDate: DateTime.now(),
      onStartEndConfirm: (DateTime start, DateTime end) {
        widget.onConfirm(start, end);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    if (widget.startTime != null) {
      _startDate = widget.startTime!;
    }else{
      _startDate=null;
    }
    if (widget.endTime != null) {
      _endDate = widget.endTime!;
    }else{
      _endDate=null;
    }
    return  Container(
      padding: EdgeInsets.only(left: 12, right: 12, top: 6, bottom: 6),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8),
        border:  Border.all(
          color: const Color(0xffE5E5E5),
          width: 1,
        )
      ),
      child: Row(
        children: [
          Icon(Icons.access_time, size: 20, color: const Color(0xff7C93C0)),
          Expanded(
            child: Text(
              _startDate == null ? '开始时间' : _startDate!.toStrYMD,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w400,
                color: _startDate == null ? const Color(0xffA0A7B5) : const Color(0xff666666),
              ),
            ).click(() {
              _selectTime(context);
            }),
          ),
          SizedBox(height: 5,),
          Text(
            '至',
            style: TextStyle(
              fontSize: 14,
              fontWeight: FontWeight.w400,
              color: Colors.black,
            ),
          ),
          SizedBox(height: 5,),
          Expanded(
            child: Text(
              _endDate == null ? '结束时间' : _endDate!.toStrYMD,
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 14,
                fontWeight: FontWeight.w400,
                color: _endDate == null ? const Color(0xffA0A7B5) : const Color(0xff666666),
              ),
            ).click((){
              _selectTime(context);
            }),
          ),
        ],
      ),
    );
  }

}
